package com.stylefeng.guns.modular.member.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.persistence.model.MemberLevel;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.IdGen;
import com.stylefeng.guns.modular.member.dao.MemberLevelDao;
import com.stylefeng.guns.modular.member.service.IMemberLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 会员等级控制器
 *
 * @author fengshuonan
 * @Date 2018-01-12 10:30:57
 */
@Controller
@RequestMapping("/memberLevel")
public class MemberLevelController extends BaseController {

    private String PREFIX = "/member/memberLevel/";

    @Autowired
    private IMemberLevelService memberLevelService;
    @Autowired
    private MemberLevelDao memberLevelDao;

    /**
     * 跳转到会员等级首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "memberLevel.html";
    }

    /**
     * 跳转到添加会员等级
     */
    @RequestMapping("/memberLevel_add")
    public String memberLevelAdd() {
        return PREFIX + "memberLevel_add.html";
    }

    /**
     * 跳转到修改会员等级
     */
    @RequestMapping("/memberLevel_update/{memberLevelId}")
    public String memberLevelUpdate(@PathVariable String memberLevelId, Model model) {
        MemberLevel memberLevel = memberLevelService.selectById(memberLevelId);
        model.addAttribute("item",memberLevel);
        return PREFIX + "memberLevel_edit.html";
    }

    /**
     * 获取会员等级列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        Page<MemberLevel> page = new PageFactory<MemberLevel>().defaultPage();
        List<MemberLevel> levels = this.memberLevelDao.list(page, condition, ShiroKit.getUser().id);
        page.setRecords(levels);
        return super.packForBT(page);
    }

    /**
     * 新增会员等级
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(MemberLevel memberLevel) {
        if(memberLevel.getMemberLevel() == null){
            throw new GunsException(BizExceptionEnum.MEMBER_LEVEL_NULL);
        }
        memberLevel.setShopId(ShiroKit.getUser().id);
        memberLevel.setId(IdGen.uuid());
        memberLevelService.insert(memberLevel);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除会员等级
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String memberLevelId) {
        memberLevelService.deleteById(memberLevelId);
        return SUCCESS_TIP;
    }

    /**
     * 修改会员等级
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(MemberLevel memberLevel) {
        memberLevelService.updateById(memberLevel);
        return super.SUCCESS_TIP;
    }

    /**
     * 会员等级详情
     */
    @RequestMapping(value = "/detail/{memberLevelId}")
    @ResponseBody
    public MemberLevel detail(@PathVariable("memberLevelId") String memberLevelId) {
        return memberLevelService.selectById(memberLevelId);
    }

}
